package com.xiaoma.cas;

import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * AtomicStampedReference和AtomicMarkableReference来解决ABA问题。 
 * java并发包中提供了一个带有标记的原子引用类AtomicStampedReference，它可以通过控制变量值的版本来保证CAS的正确性。
 * https://www.jianshu.com/p/24ffe531e9ee
 * @author xiaomacdut
 * @date 2020年2月22日 下午3:54:58
 */
public class AtomicStampedReferenceDemo{
    
    public static void main(String [] args){
    String str1 = "aaa";
    String str2 = "bbb";
    AtomicStampedReference<String> reference = new AtomicStampedReference<String>(str1,1);
    reference.compareAndSet(str1,str2,reference.getStamp(),reference.getStamp()+1);
    
    System.out.println("reference.getReference() = " + reference.getReference());

    boolean b = reference.attemptStamp(str2, reference.getStamp() + 1);
        
    System.out.println("b: "+b);
    System.out.println("reference.getStamp() = "+reference.getStamp());

    boolean c = reference.weakCompareAndSet(str2,"ccc",4, reference.getStamp()+1);
    System.out.println("reference.getReference() = "+reference.getReference());
    System.out.println("c = " + c);
    }

}
